<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/7/25 0025
 * Time: 7:36
 */

namespace app\admin\controller;
use app\service\CategoryService;
use app\service\SupplierService;
use think\Db;
use think\facade\Debug;

/**
 * 供应商管理.
 * @author   juzi
 * @blog    https://blog.csdn.net/juziaixiao
 * @version  0.0.1
 * @datetime 2020年7月25日07:38:52
 */
class Supplier extends Common
{
    /**
     * 构造方法.
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月25日07:38:52
     */
    public function __construct()
    {
        // 调用父类前置方法
        parent::__construct();

        $this->IsLogin();
    }

    /**
     * [Index 列表].
     * @author   juzi
     * @blog     https://blog.csdn.net/juziaixiao
     * @version  1.0.0
     * @datetime 2020年7月22日10:58:48
     */
    public function Index()
    {


//        $re=Db::name('supplier_class')->field('class_id,id')->select();
//        $re1=Db::name('bizpro_class')->column('id');
//        foreach ($re as $v) {
//            if (!in_array($v['class_id'],$re1) ) {
//                Db::name('supplier_class')->where('id='.$v['id'])->delete();
//            }
//        }
//
//        exit;
       //$data=[];

       // Db::name('supplier_class')->insertAll($data);
        if ('ajax' != input('get.action')) {
            $this->assign('from_client', input('get.from_client'));
            $this->assign('audit_status', input('get.audit_status'));
            return $this->fetch();
        }
        else {
//            $searchParams = input('post.searchParams');
            // 参数
            $params = $this->data_post;
            $param = $this->data_get;
            if(isset($param['audit_status'])&&$param['audit_status']>0)
            {
                $params['param']['audit_status']=$param['audit_status'];
            }
            if(isset($param['from_client'])&&$param['from_client']>0)
            {
                $params['param']['from_client']=$param['from_client'];
            }
           // dump($param['from_client']==0);exit;
            // 条件
            $where = SupplierService::supListWhere($params['param']);

            $data_params = [
                'where' => $where,
                'page' => true,
                'field' => 'id,supplier_name,bizpro_class,relation_name,relation_phone,desc,audit_status',
                'number' => 10,
                'table'=>'supplier'
            ];


            $res=SupplierService::DataList($data_params);


            $re = SupplierService::DataDealWith($res);

            $total = SupplierService::DataTotal('supplier',$where);

            return ['code' => 0, 'msg' => '', 'count' => $total, 'data' => $re['data']];
        }
    }

    /**
     * [Saveinfo 供应商编辑页面].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月25日11:10:10
     */
    public function Saveinfo()
    {
        // 参数
        $params = input();
        // 数据
        $data = [];
        if (!empty($params['id'])) {
            // 获取列表
            $data_params = [
                'm' => 0,
                'n' => 1,
                'page' => false,
                'where' => ['id' => intval($params['id'])],
                'field' => '*',
                'table'=>'supplier'
            ];

            $res=SupplierService::DataList($data_params);
            $re = SupplierService::DataDealWith($res);
            $data = empty($re['data'][0]) ? [] : $re['data'][0];
        }
        //dump($data);exit;
        $data['catestr']=CategoryService::HandleCate()['catestr'];
        $data['catearr']=CategoryService::HandleCate()['catearr'];

        $this->assign('res', $data);

        return $this->fetch();
    }


    /**
     * [Save 快递添加/编辑].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年6月23日09:57:30
     */
    public function Save()
    {

        // 是否ajax
        if (!IS_AJAX) {
            return $this->error('非法访问');
        }

        // 开始操作
        $params = $this->data_post;

        //数据校验
        SupplierService::VaryData($params);
        //整理数据
        //$data=SupplierService::HandleData($params);

        SupplierService::SaveSup($params);

        return DataReturn('保存成功', 0);
    }

    /**
     * [删除供应商].
     * @author   juzi
     * @blog    https://blog.csdn.net/juziaixiao
     * @version 1.0.0
     * @date    2020年7月21日17:02:36
     */
    public function DelInfo()
    {
        // 是否ajax
        if (!IS_AJAX)
        {
            return $this->error('非法访问');
        }

        $params=[
            'id'=>input('post.id'),
            'table'=>'supplier',
            'errorcode'=>'9999',
            'msg'=>'删除供应商失败'
        ];
        SupplierService::VeryBeforeDel($params);
        SupplierService::DelInfo($params);
        return DataReturn('删除成功', 0);
    }


    public function get_ip()
    {


    }
}